package com.example.myblog1_0.controller;

import com.example.myblog1_0.mapper.UserMapper;
import com.example.myblog1_0.model.Article;
import com.example.myblog1_0.model.MyUser;
import com.example.myblog1_0.service.ArticleService;
import com.example.myblog1_0.service.CategoryService;
import com.example.myblog1_0.util.BuildArticleSummaryUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: xc
 * @Date: 2018/12/16 18:57
 * @Description:
 **/
@RestController
public class EditorController {
    @Autowired
    ArticleService articleService;
    @Autowired
    UserMapper userMapper;
    /**
     * 储存文章
     * @param article
     * @param request
     * @return
     */
    @PostMapping("/publishArticle")
    public JSONObject saveArticle(Article article,
                                  HttpServletRequest request,
                                  @AuthenticationPrincipal Principal principal){
        JSONObject returnObject = new JSONObject();
        BuildArticleSummaryUtil summaryUtil = new BuildArticleSummaryUtil();
        String summary = summaryUtil.BuildArticleSummary(request.getParameter("articleHtmlContent"));
        article.setSummary(summary + "...");
        String username = null;
        try {
            username = principal.getName();
        }catch (NullPointerException e) {
            returnObject.put("status",403);
            return returnObject;
        }
        if(!"ROLE_ADMIN".equals(userMapper.findUserInfoByName(username).getRole())){
            returnObject.put("status",500);
            return returnObject;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String writeTime = sdf.format(new Date());
        article.setWriteTime(writeTime);

        boolean flag = articleService.insetArticle(article);
        Article findArticle = articleService.findArticleByTime(writeTime);
        if (flag){
            findArticle.setArticleUrl("https://www.abstergo.xin/findArticle?articleID=" + findArticle.getId());
            articleService.updateUrlByTime(findArticle);
            returnObject.put("status",200);
            returnObject.put("articleTitle",findArticle.getTitle());
            returnObject.put("writeTime",writeTime);
            returnObject.put("articleUrl","/findArticle?articleID=" + findArticle.getId());
            return returnObject;
        }
        else {
            returnObject.put("status",0);
            return returnObject;
        }
    }
    @GetMapping("/findArticleById")
    public JSONObject findArticleById(@RequestParam("id") String id){
        return articleService.getArticleByArticleId(id);
    }
    @PostMapping("/updateArticleByID")
    public JSONObject updateArticleByID(Article article){
        return articleService.updateArticleByID(article);
    }
    @GetMapping("/canYouWrite")
    public int canYouWrite(@AuthenticationPrincipal Principal principal){
        String username = principal.getName();
        MyUser findUser = userMapper.findUserInfoByName(username);
        if ("ROLE_ADMIN".equals(findUser.getRole())){
            return 1;
        }else {
            return 0;
        }
    }
}
